package com.ztk.threadpool;

import org.apache.tomcat.util.threads.ThreadPoolExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.atomic.AtomicInteger;

@RestController
@RequestMapping("/thread-pool")
public class ThreadPoolDemo {

    private final AtomicInteger atomicInteger = new AtomicInteger(0);

    /**
     * 测试10个线程的处理
     *
     * @return
     */
    @GetMapping("/10-pool")
    public String getPool10(){
        ThreadPoolExecutor threadPoolExecutor = ThreadPoolUtil.threadPoolExecutor10();
        threadPoolExecutor.execute(() -> {
            atomicInteger.incrementAndGet();
            if (atomicInteger.get() == 9) {
                System.out.println(atomicInteger.get());
            }
            try {
                Thread.sleep(1100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        return "success";
    }


    /**
     * 测试100个线程的处理
     *
     * @return
     */
    @RequestMapping("/100-pool")
    public String getPool100(){
        ThreadPoolExecutor threadPoolExecutor = ThreadPoolUtil.threadPoolExecutor100();
        threadPoolExecutor.execute(() -> {
            atomicInteger.incrementAndGet();
            if (atomicInteger.get() == 95) {
                System.out.println(atomicInteger.get());
            }
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        return "success";
    }
}
